<%@page import="java.net.URLDecoder"%>
<%@page import="cn.kgcnews.pojo.User"%>
<%@page import="cn.kgcnews.service.impl.UserServiceImpl"%>
<%@page import="cn.kgcnews.service.UserService"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div class="container">
    <nav class="navbar navbar-default" role="navigation">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">新闻大视野</a>
            </div>
            <div>
                <ul class="nav navbar-nav">
                    <li class="active"><a href="index.html">首页</a></li>
                    <li><a href="#">国内</a></li>
                    <li><a href="#">国际</a></li>
                    <li><a href="#">娱乐</a></li>
                    <li><a href="#">体育</a></li>
                </ul>
            </div>
            <form class="navbar-form navbar-left" role="search">
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="请输入搜索关键词">
                </div>
                <button type="submit" class="btn btn-default">搜索</button>
            </form>
            <ul class="nav navbar-nav navbar-right">
               <%
						String userName = null;
						String userPass = null;
						//读取客户端存储的本网站的所有cookie
						Cookie[] cookies = request.getCookies();
						if (cookies != null) {
							for (Cookie c : cookies) {
								if ("code".equals(c.getName())) {
								//将此页面中获取的coolie中的内容编译成utf-8的编码方式显示
								String code = URLDecoder.decode(c.getValue(), "UTF-8");
									String[] str = code.split("&");
									userName = str[0];
									userPass = str[1];
								}
							}
						}
						UserService userService = new UserServiceImpl();
						//验证用户名和密码
						User user = userService.login(userName, userPass);
						request.setAttribute("user", user);
					%>
				<c:if test="${empty user}">
				 <li><a data-toggle="modal" data-target="#register" href=""><span class="glyphicon glyphicon-user"></span>注册</a></li>
                 <li><a data-toggle="modal" data-target="#login" href=""><span class="glyphicon glyphicon-log-in"></span>登录</a></li>
				</c:if>
				<c:if test="${!empty user}">
				  <li><a>欢迎您，${user.userName}</a></li>
                  <li><a href="douser?action=out">注销</a></li>
				</c:if>
            </ul>
            <!-- 注册窗口 -->
            <div id="register" class="modal fade" tabindex="-1">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-body">
                            <button class="close" data-dismiss="modal">
                                <span>&times;</span>
                            </button>
                        </div>
                        <div class="modal-title">
                            <h1 class="text-center">注册</h1>
                        </div>
                        <div class="modal-body">
                            <form class="form-group" action="douser" method="post">
                            <input type="hidden" name="action" value="reg">
                                <div class="form-group">
                                    <label for="userName">用户名</label>
                                    <input id="userName" name="userName" class="form-control" type="text" required placeholder="6-15位字母或数字">
                                </div>
                                <div class="form-group">
                                    <label for="userPwd">密码</label>
                                    <input id="userPwd" name="userPwd" class="form-control" type="password" required placeholder="至少6位字母或数字">
                                </div>
                                <div class="form-group">
                                    <label for="repwd">再次输入密码</label>
                                    <input id="repwd" class="form-control" type="password" placeholder="至少6位字母或数字">
                                </div>
                                <div class="form-group">
                                    <label for="email">邮箱</label>
                                    <input id="email" name="email" class="form-control" type="email" required placeholder="例如:123@123.com">
                                </div>                     
                                <div class="text-right">
                                    <button class="btn btn-primary" type="submit">保存</button>
                                    <button class="btn btn-danger" data-dismiss="modal">取消</button>
                                </div>
                                <a href="" data-toggle="modal" data-dismiss="modal" data-target="#login">已有账号？点我登录</a>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 登录窗口 -->
            <div id="login" class="modal fade">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-body">
                            <button class="close" data-dismiss="modal">
                                <span>&times;</span>
                            </button>
                        </div>
                        <div class="modal-title">
                            <h1 class="text-center">登录</h1>
                        </div>
                        <div class="modal-body">
                            <form name="" class="form-group" action="douser" method="post">
                            <input type="hidden" name="action" value="log">
                                <div class="form-group">
                                    <label for="userName">用户名</label>
                                    <input id="userName" name="userName" class="form-control" type="text" placeholder="">
                                </div>
                                <div class="form-group">
                                    <label for="userPwd">密码</label>
                                    <input id="userPwd" name="userPwd" class="form-control" type="password" placeholder="">
                                </div>
                                <div class="text-right">
                                    <button class="btn btn-primary" type="submit">登录</button>
                                    <button class="btn btn-danger" data-dismiss="modal">取消</button>
                                </div>
                                <a href="" data-toggle="modal" data-dismiss="modal" data-target="#register">还没有账号？点我注册</a>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </nav>
</div>